package com.example.lockandqueue;

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * 有界阻塞队列
 */
public class ArrayBlockingQueueMain {

    ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(10);

    public ArrayBlockingQueue<String> getQueue() {
        return queue;
    }

    public void setQueue(ArrayBlockingQueue<String> queue) {
        this.queue = queue;
    }

    public static void main(String[] args) {
        ArrayBlockingQueueMain arrayBlockingQueueMain = new ArrayBlockingQueueMain();
        ArrayBlockingQueue<String> queue = arrayBlockingQueueMain.getQueue();
        new Thread(()->{
            while (true){
                try {
                    int i = new Random().nextInt();
                    queue.put(i + "");
                    System.out.println("put"+i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(()->{
            while (true){
                String poll = queue.poll();
                System.out.println("poll"+poll);
//                int size = queue.size();
//                System.out.println(size);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }

}
